package view.GUI.sidepanels;

import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author Arfeniel, modified by Ben
 */
public class PropertiesTable extends JTable {

    TableCellRenderer cr = (TableCellRenderer) new ColorRenderer();
    TableCellRenderer ur = (TableCellRenderer) new UneditableRenderer();
    TableCellRenderer lr = (TableCellRenderer) new LocationRenderer();
    TableCellRenderer labr = (TableCellRenderer) new LabelRenderer();
    TableCellRenderer br = (TableCellRenderer) new BooleanRenderer();
    TableCellEditor ce = (TableCellEditor) new ColorEditor();
    TableCellEditor le = (TableCellEditor) new LocationEditor();
    TableCellEditor labe = (TableCellEditor) new LabelEditor();
    TableCellEditor be = (TableCellEditor) new BooleanEditor();
    private PropertiesState modus;

    PropertiesTable(PropertiesTableModel ptm, PropertiesState modus) {
        super(ptm);
        this.setGridColor(Color.LIGHT_GRAY);
        this.modus = modus;
    }

    @Override
    public TableCellEditor getCellEditor(int row, int column) {
        TableCellEditor editor = null;
         if ((row > 2 && row < 5) && (column == 1)) {
            editor =  ce;
        } else if (row == 0 && column == 1) {
            editor =  labe;
        } else if (row == 1 && column == 1) {
            editor =  be;
        } else if (row == 6 && column == 1 && (PropertiesStateAbstractInPort.class.isAssignableFrom(modus.getClass())
                 || PropertiesStateAbstractOutPort.class.isAssignableFrom(modus.getClass()))) {
            editor =  be;
        }else if (row == 5 && column == 1) {
            editor =  le;
        }else {
            editor =  super.getCellEditor(row, column);
        }
        return editor;
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        TableCellRenderer renderer = null;
        if ((row > 2 && row < 5) && (column == 1)) {
            renderer =  cr;
        }else if (row == 0 && column == 1) {
            renderer =  labr;
        }else if (row == 1 && column == 1) {
            renderer =  br;
        }else if (row == 6 && column == 1 && (PropertiesStateAbstractInPort.class.isAssignableFrom(modus.getClass())
                 || PropertiesStateAbstractOutPort.class.isAssignableFrom(modus.getClass()))) {
            renderer =  br;
        }else if (row == 5 && column == 1) {
            renderer =  lr;
        }else if (column != 0 && !this.isCellEditable(row, column)) {
            renderer =  ur;
        } else {
            renderer =  super.getCellRenderer(row, column);
        }
        return renderer;
    }
}
